# Maintainer: Markus Rickert <rickert@fortiss.org>

_realname=ode
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.16.2
pkgrel=2
pkgdesc="A library for simulating articulated rigid body dynamics (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://bitbucket.org/odedevs/ode"
license=('LGPL', 'BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('strip' 'staticlibs')
source=("https://bitbucket.org/odedevs/ode/downloads/${_realname}-${pkgver}.tar.gz"
        '010-float.patch::https://bitbucket.org/odedevs/ode/commits/92eb12af400671ebf90860b4af2d8755262d1b8d/raw'
        '020-float.patch')
sha256sums=('b26aebdcb015e2d89720ef48e0cb2e8a3ca77915f89d853893e7cc861f810f22'
            'eb59cc5c1f8c40ab7eb40b99b188a0c68c03863b14764c503c8c6ea625cc8ec3'
            'e21c1e9f24b6df5086d5e8927c494c061830c3681b413f79e782f9c0b606b9d3')

prepare() {
  cd $srcdir/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/010-float.patch
  patch -p1 -i ${srcdir}/020-float.patch
}

build() {
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}"/build-${CARCH}

  ${MINGW_PREFIX}/bin/ctest
}

package() {
  cd "${srcdir}"/build-${CARCH}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE-BSD.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-BSD.txt"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/GIMPACT/GIMPACT-LICENSE-BSD.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/GIMPACT/GIMPACT-LICENSE-BSD.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/GIMPACT/GIMPACT-LICENSE-LGPL.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/GIMPACT/GIMPACT-LICENSE-LGPL.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/libccd/BSD-LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/libccd/BSD-LICENSE"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/OPCODE/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/OPCODE/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/ou/LICENSE.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ou/LICENSE.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/ou/LICENSE-BSD.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ou/LICENSE-BSD.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/ou/LICENSE-LESSER.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ou/LICENSE-LESSER.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/ou/LICENSE-ZLIB.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ou/LICENSE-ZLIB.TXT"
}
